//
//  ActionSheetVIew.m
//  MPTLive
//
//  Created by HouGeng on 15/11/17.
//  Copyright © 2015年 Jeakin. All rights reserved.
//

#import "ActionSheetVIew.h"

/// 通过RGBA设置颜色，使用0x格式，如：RGBAAllColor(0xAABBCC, 0.5);
#define RGBAAllColor(rgb, a) [UIColor colorWithRed:((float)((rgb & 0xFF0000) >> 16))/255.0  \
green:((float)((rgb & 0xFF00) >> 8))/255.0     \
blue:((float)(rgb & 0xFF))/255.0              \
alpha:(a)/1.0]

#define ButHeight (53)

@interface ActionSheetVIew()
{
    /// 线
    UILabel *labLine1;
    /// 线
    UILabel *labLine2;
}

/// 拍照
@property (nonatomic, strong) UIButton *butPanZhao;

/// 相册
@property (nonatomic, strong) UIButton *butXiangChe;

/// 取消
@property (nonatomic, strong) UIButton *butQuXiao;


@end

@implementation ActionSheetVIew


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        
        /// Y值
        CGFloat fltY = self.frame.size.height;
        
        /// 拍照
        _butPanZhao = [UIButton buttonWithType:UIButtonTypeSystem];
        _butPanZhao.frame = CGRectMake(0, fltY, self.frame.size.width, ButHeight);
        [_butPanZhao addTarget:self action:@selector(butClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_butPanZhao setTitle:@"拍照" forState:UIControlStateNormal];
        [_butPanZhao setTitleColor:RGBAAllColor(0x282828, 1) forState:UIControlStateNormal];
        _butPanZhao.titleLabel.font = [UIFont systemFontOfSize:16];
        _butPanZhao.backgroundColor = [UIColor whiteColor];
        [self addSubview:_butPanZhao];
    
        /// 线
        labLine1 = [[UILabel alloc] initWithFrame:CGRectMake(0, ButHeight +fltY, self.frame.size.width, 0.5)];
        labLine1.backgroundColor = RGBAAllColor(0xEEEEEE, 1);
        [self addSubview:labLine1];
        
        /// 相册
        _butXiangChe = [UIButton buttonWithType:UIButtonTypeSystem];
        _butXiangChe.frame = CGRectMake(0, ButHeight + 0.5 +fltY, self.frame.size.width, ButHeight);
        [_butXiangChe addTarget:self action:@selector(butClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_butXiangChe setTitle:@"从手机相册选择" forState:UIControlStateNormal];
        [_butXiangChe setTitleColor:RGBAAllColor(0x282828, 1) forState:UIControlStateNormal];
        _butXiangChe.titleLabel.font = [UIFont systemFontOfSize:16];
        _butXiangChe.backgroundColor = [UIColor whiteColor];
        [self addSubview:_butXiangChe];
        
        /// 线
        labLine2 = [[UILabel alloc] initWithFrame:CGRectMake(0, ButHeight * 2 + 0.5+fltY, self.frame.size.width, 5)];
        labLine2.backgroundColor = RGBAAllColor(0xEEEEEE, 1);
        [self addSubview:labLine2];
        
        
        /// 取消
        _butQuXiao = [UIButton buttonWithType:UIButtonTypeSystem];
        _butQuXiao.frame = CGRectMake(0, ButHeight * 2 + 0.5+fltY + 5, self.frame.size.width, ButHeight);
        [_butQuXiao addTarget:self action:@selector(butClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_butQuXiao setTitle:@"取消" forState:UIControlStateNormal];
        [_butQuXiao setTitleColor:RGBAAllColor(0x282828, 1) forState:UIControlStateNormal];
        _butQuXiao.titleLabel.font = [UIFont systemFontOfSize:16];
        _butQuXiao.backgroundColor = [UIColor whiteColor];
        [self addSubview:_butQuXiao];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClicked:)];
        [self addGestureRecognizer:tap];
        
        [self showDongHua];
    }
    return self;
}

/// 动画
- (void)showDongHua
{
    /// Y值
    CGFloat fltY = self.frame.size.height - (53 * 3 + 5 + 0.5);
    
    [UIView animateWithDuration:0.3 animations:^{
        
        _butPanZhao.frame = CGRectMake(0, fltY, self.frame.size.width, ButHeight);
        labLine1.frame =CGRectMake(0, ButHeight +fltY, self.frame.size.width, 0.5);
        _butXiangChe.frame = CGRectMake(0, ButHeight + 0.5 +fltY, self.frame.size.width, ButHeight);
        labLine2.frame =CGRectMake(0, ButHeight * 2 + 0.5+fltY, self.frame.size.width, 5);
         _butQuXiao.frame = CGRectMake(0, ButHeight * 2 + 0.5+fltY + 5, self.frame.size.width, ButHeight);
    }];
}

-(void)butClicked:(UIButton *)but
{
    if (but == _butPanZhao)
    {
        /// 拍照
        self.block(1);
    }
    else if (but == _butXiangChe)
    {
        /// 相册
        self.block(2);
    }
    else
    {
        ///取消
        /// Y值
        CGFloat fltY = self.frame.size.height;
        [UIView animateWithDuration:0.3 animations:^{
            
            _butPanZhao.frame = CGRectMake(0, fltY, self.frame.size.width, ButHeight);
            labLine1.frame =CGRectMake(0, ButHeight +fltY, self.frame.size.width, 0.5);
            _butXiangChe.frame = CGRectMake(0, ButHeight + 0.5 +fltY, self.frame.size.width, ButHeight);
            labLine2.frame =CGRectMake(0, ButHeight * 2 + 0.5+fltY, self.frame.size.width, 5);
            _butQuXiao.frame = CGRectMake(0, ButHeight * 2 + 0.5+fltY + 5, self.frame.size.width, ButHeight);
            
        } completion:^(BOOL finished) {
            
            [self removeFromSuperview];
            
        }];
    }
}

/// 关闭事件
- (void)tapClicked:(UITapGestureRecognizer *)tap
{
    /// Y值
    CGFloat fltY = self.frame.size.height;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        _butPanZhao.frame = CGRectMake(0, fltY, self.frame.size.width, ButHeight);
        labLine1.frame =CGRectMake(0, ButHeight +fltY, self.frame.size.width, 0.5);
        _butXiangChe.frame = CGRectMake(0, ButHeight + 0.5 +fltY, self.frame.size.width, ButHeight);
        labLine2.frame =CGRectMake(0, ButHeight * 2 + 0.5+fltY, self.frame.size.width, 5);
        _butQuXiao.frame = CGRectMake(0, ButHeight * 2 + 0.5+fltY + 5, self.frame.size.width, ButHeight);
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];

}

@end
